package com.jf.classes.service.impl;

import java.util.List;
import com.jf.common.utils.DateUtils;
import com.jf.students.domain.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.jf.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.jf.classes.mapper.ClassesMapper;
import com.jf.classes.domain.Classes;
import com.jf.classes.service.IClassesService;

/**
 * 班级信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-13
 */
@Service
public class ClassesServiceImpl implements IClassesService
{
    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 查询班级信息
     *
     * @param id 班级信息主键
     * @return 班级信息
     */
    @Override
    public Classes selectClassesById(Long id)
    {
        return classesMapper.selectClassesById(id);
    }

    /**
     * 查询班级信息列表
     *
     * @param classes 班级信息
     * @return 班级信息
     */
    @Override
    public List<Classes> selectClassesList(Classes classes)
    {
        return classesMapper.selectClassesList(classes);
    }

    /**
     * 新增班级信息
     *
     * @param classes 班级信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertClasses(Classes classes)
    {
        classes.setCreateTime(DateUtils.getNowDate());
        int rows = classesMapper.insertClasses(classes);
        insertStudents(classes);
        return rows;
    }

    /**
     * 修改班级信息
     *
     * @param classes 班级信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateClasses(Classes classes)
    {
        classesMapper.deleteStudentsByClassId(classes.getId());
        insertStudents(classes);
        return classesMapper.updateClasses(classes);
    }

    /**
     * 批量删除班级信息
     *
     * @param ids 需要删除的班级信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteClassesByIds(Long[] ids)
    {
        classesMapper.deleteStudentsByClassIds(ids);
        return classesMapper.deleteClassesByIds(ids);
    }

    /**
     * 删除班级信息信息
     *
     * @param id 班级信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteClassesById(Long id)
    {
        classesMapper.deleteStudentsByClassId(id);
        return classesMapper.deleteClassesById(id);
    }

    /**
     * 新增学生信息信息
     *
     * @param classes 班级信息对象
     */
    public void insertStudents(Classes classes)
    {
        List<Students> studentsList = classes.getStudentsList();
        Long id = classes.getId();
        if (StringUtils.isNotNull(studentsList))
        {
            List<Students> list = new ArrayList<Students>();
            for (Students students : studentsList)
            {
                students.setClassId(id);
                list.add(students);
            }
            if (list.size() > 0)
            {
                classesMapper.batchStudents(list);
            }
        }
    }
}
